package com.cskaoyan.service.mall;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketKeyword;
import com.cskaoyan.bean.po.MarketKeywordExample;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MallKeywordListServicelmpl implements MallKeywordListService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketKeyword> queryMallKeywordList(BasePageInfo basePageInfo, String keyword, String url) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if (!StringUtil.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword.trim() + "%");
        }
        if (!StringUtil.isEmpty(url)) {
            criteria.andUrlLike("%" + url.trim() + "%");
        }
        marketKeywordExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);

        PageInfo<MarketKeyword> marketKeywordPageInfo = new PageInfo<>(marketKeywords);

        return CommonData.data(marketKeywordPageInfo);
    }

    @Override
    public MarketKeyword createMallKeyword(MarketKeyword marketKeyword) {
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        int code = marketKeywordMapper.insertSelective(marketKeyword);
        if (code == 0) {
            return null;
        }
        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return marketKeyword1;
    }

    @Override
    public MarketKeyword updateMallKeyword(MarketKeyword marketKeyword) {
        marketKeyword.setUpdateTime(new Date());
        int code = marketKeywordMapper.updateByPrimaryKeySelective(marketKeyword);
        if (code == 0) {
            return null;
        }
        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(marketKeyword.getId());
        return marketKeyword1;
    }

    @Override
    public int deleteMallKeyword(Integer id) {
        int code = marketKeywordMapper.deleteByPrimaryKey(id);
        return code;
    }
}
